{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "6930096b-46ae-425a-87e5-593095f067b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# 读取数据集 1分\n",
    "data = pd.read_csv('patient_data.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "4037d775-8627-4e9c-8522-04f122ff8aea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['PatientID', 'Age', 'BMI', 'BloodPressure', 'Cholesterol',\n",
       "       'DaysInHospital', 'RiskLevel', 'BMIRange'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "cd5a5986-772a-41e4-95d3-1ce2929a0f09",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "高风险患者数量: 413\n",
      "低风险患者数量: 587\n",
      "高风险患者占比: 0.413\n",
      "低风险患者占比: 0.587\n"
     ]
    }
   ],
   "source": [
    "# 1. 统计住院天数超过7天的患者数量及其占比\n",
    "# 创建新列'RiskLevel'，根据住院天数判断风险等级 3分\n",
    "data['RiskLevel']= np.where(data['DaysInHospital']>7, '高风险患者', '低风险患者')\n",
    "# 统计不同风险等级的患者数量 2分\n",
    "risk_counts = data['RiskLevel'].value_counts()\n",
    "# 计算高风险患者占比 1分\n",
    "high_risk_ratio = risk_counts['高风险患者'] / len(data)\n",
    "# 计算低风险患者占比 1分\n",
    "low_risk_ratio = risk_counts['低风险患者'] / len(data)\n",
    "\n",
    "# 输出结果\n",
    "print(\"高风险患者数量:\", risk_counts['高风险患者'])\n",
    "print(\"低风险患者数量:\", risk_counts['低风险患者'])\n",
    "print(\"高风险患者占比:\", high_risk_ratio)\n",
    "print(\"低风险患者占比:\", low_risk_ratio)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "32511bf1-2155-44b1-a0ac-252b711da6d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "BMI区间中高风险患者的比例和患者数:\n",
      "BMIRange\n",
      "偏瘦    0.444444\n",
      "正常    0.406699\n",
      "超重    0.388235\n",
      "肥胖    0.415094\n",
      "Name: RiskLevel, dtype: float64\n",
      "BMIRange\n",
      "肥胖    477\n",
      "正常    209\n",
      "超重    170\n",
      "偏瘦    144\n",
      "Name: count, dtype: int64\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Chris\\AppData\\Local\\Temp\\ipykernel_19040\\1541973307.py:8: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "  bmi_risk_rate = data.groupby(data['BMIRange'])['RiskLevel'].apply(lambda x: (x == '高风险患者').mean())\n"
     ]
    }
   ],
   "source": [
    "# 2. 统计不同BMI区间中高风险患者的比例和统计不同BMI区间中的患者数\n",
    "# 定义BMI区间和标签\n",
    "bmi_bins = [0, 18.5, 24, 28, np.inf]\n",
    "bmi_labels = ['偏瘦', '正常', '超重', '肥胖']\n",
    "# 根据BMI值划分指定区间 4分\n",
    "data['BMIRange'] = pd.cut(data['BMI'],bins=bmi_bins,labels=bmi_labels, right=False)  # 使用左闭右开区间\n",
    "# 计算每个BMI区间中高风险患者的比例 2分\n",
    "bmi_risk_rate = data.groupby(data['BMIRange'])['RiskLevel'].apply(lambda x: (x == '高风险患者').mean())\n",
    "# 统计每个BMI区间的患者数量 1分\n",
    "bmi_patient_count = data['BMIRange'] .value_counts()\n",
    "\n",
    "# 输出结果\n",
    "print(\"BMI区间中高风险患者的比例和患者数:\")\n",
    "print(bmi_risk_rate) \n",
    "print(bmi_patient_count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "efc6523c-0bc2-46c2-8810-b0ee604d6f0c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "年龄区间中高风险患者的比例和患者数:\n",
      "AgeRange\n",
      "≤25岁      0.456693\n",
      "26-35岁    0.398496\n",
      "36-45岁    0.386364\n",
      "46-55岁    0.444444\n",
      "56-65岁    0.401575\n",
      "＞65岁      0.401254\n",
      "Name: RiskLevel, dtype: float64\n",
      "AgeRange\n",
      "＞65岁      319\n",
      "46-55岁    162\n",
      "26-35岁    133\n",
      "36-45岁    132\n",
      "≤25岁      127\n",
      "56-65岁    127\n",
      "Name: count, dtype: int64\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Chris\\AppData\\Local\\Temp\\ipykernel_19040\\1466217758.py:8: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "  age_risk_rate = data.groupby(data['AgeRange'])['RiskLevel'].apply(lambda x: (x == '高风险患者').mean())\n"
     ]
    }
   ],
   "source": [
    "# 3. 统计不同年龄区间中高风险患者的比例和统计不同年龄区间中的患者数\n",
    "# 定义年龄区间和标签\n",
    "age_bins = [0, 26, 36, 46, 56, 66, np.inf]\n",
    "age_labels = ['≤25岁', '26-35岁', '36-45岁', '46-55岁', '56-65岁', '＞65岁']\n",
    "# 根据年龄值划分指定区间 4分\n",
    "data['AgeRange'] = pd.cut(data['Age'], bins=age_bins, labels=age_labels, right=False)  # 使用左闭右开区间\n",
    "# 计算每个年龄区间中高风险患者的比例 2分\n",
    "age_risk_rate = data.groupby(data['AgeRange'])['RiskLevel'].apply(lambda x: (x == '高风险患者').mean())\n",
    "# 统计每个年龄区间的患者数量 1分\n",
    "age_patient_count = data['AgeRange'].value_counts()\n",
    "\n",
    "# 输出结果\n",
    "print(\"年龄区间中高风险患者的比例和患者数:\")\n",
    "print(age_risk_rate) \n",
    "print(age_patient_count) \n",
    "data.to_csv('new.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4fd5dfca-eab6-47b6-a420-ccd25e3198fd",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a61c406-3423-4cfc-bb32-a75e3dfe86a3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e3f596d3-d336-45fd-bd4d-1cafcb04ca54",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c7df2b9-6e39-4b30-b560-cda01aa56f05",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a4d3bf6-17a5-43b6-8877-6b44c6ee7914",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
